<style scoped>
</style>

<template>
    <div class="company">
        <fieldset class="layui-elem-field layui-field-title">
            <legend>修改部门</legend>
        </fieldset>

        <form class="layui-form " >
            <div class="layui-form-item">
                <label class="layui-form-label">名称</label>
                <div class="layui-input-block">
                    <input type="text" name="name" :value="adata.name" required lay-verify="required" placeholder="" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">编码</label>
                <div class="layui-input-block">
                    <input type="text" name="code" :value="adata.code" required lay-verify="required" placeholder="" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item layui-form-text">
                <label class="layui-form-label">描述</label>
                <div class="layui-input-block">
                    <textarea name="description" :value="adata.description" placeholder="请输入描述内容" class="layui-textarea"></textarea>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">所属公司</label>
                <div class="layui-input-inline">
                    <select name="orgguid" id="orgguid" lay-verify="required">
                        <option value="1">销售公司</option>
                        <option value="2">土豆集</option>
                        <option value="3">其他</option>
                    </select>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">负责人</label>
                <div class="layui-input-block">
                    <input type="text" name="manager" :value="adata.manager" required lay-verify="required" placeholder="" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">负责人电话</label>
                <div class="layui-input-block">
                    <input type="text" name="managertel" :value="adata.managertel" required lay-verify="required" placeholder="" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item layui-form-text">
                <label class="layui-form-label">备注</label>
                <div class="layui-input-block">
                    <textarea name="remark" :value="adata.remark" placeholder="请输入备注内容" class="layui-textarea"></textarea>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn" lay-submit lay-filter="submit">立即提交</button>
                    <button class="layui-btn layui-btn-primary" @click="$parent.back">返回</button>
                </div>
            </div>
        </form>
    </div>
</template>

<script>
    export default {
        name: 'EditDepartment',
        props: ['id'],
        data () {
            return {
                 guid : this.id,
                 adata: {}
            }
        },
        mounted() {
            let that = this;
            layui.use(['form'], function () {
                var form = layui.form,
                    layer = layui.layer;

                let loader = layer.load();
                //获取回显数据
                $.ajax({
                    url: '/api/department/edit',
                    data: {guid: that.guid},
                    type: "post",
                    dataType: "json",
                    success: function(res) {
                        if (res.info != null) {
                            that.adata = res.info;
                            $('#orgguid').val(res.info.orgguid);
                            form.render('select');
                        } else {
                            that.toast("系统错误");
                        }
                    },
                    complete: function () {
                        layer.close(loader);
                    }
                });
                //提交编辑过的数据
                form.on('submit(submit)', function (data) {
                    let loader = layer.load();
                    $.ajax({
                        url: '/api/department/add',
                        data: JSON.stringify(data.field),
                        type: "post",
                        dataType: "json",
                        success: function(res) {
                            if (res.info == "ok") {
                                that.toast("修改成功");
                                that.$parent.switchPage('Department');
                            } else {
                                that.toast("系统错误");
                            }
                        },
                        complete: function () {
                            layer.close(loader);
                        }
                    });
                    return false;
                });
            });

        },
        methods: {
            back: function () {
                this.$parent.switchPage(this.$parent.refView);
            }
        }
    }
</script>
